
/**
 * @author zhaoren
 * @version 1.0
 * @description TODO
 * @date 2020/7/29 15:44
 **/
public class List<T> implements Iterable<T>{

    private Object[] objects;
    private static final int DEFAULT_SIZE = 1000;
    private int ptr = 0;
    private int size = 0;

    public List(){
        this(DEFAULT_SIZE);
    }

    public List(int size){
        objects = (T[]) new Object[size];
    }

    public void add(T t){
        objects[ptr++] = t;
        size++;
    }

    /**
     * 返回一个迭代器
     * @return
     */
    public Iterator<T> iterator() {
        return new Itr();
    }

    private class Itr implements Iterator<T> {

        private int current = 0;

        public T first() {
            if (hasNext())return (T) objects[0];
            throw new NullPointerException();
        }

        public T next() {
            T object = (T) objects[current];
            current++;
            return object;
        }

        public boolean hasNext() {
            return current + 1 <= size ? true : false;
        }
    }
}
